package org.freeone.learning.springbootsecurity.util;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder(alphabetic=true)
public final class ApiResult<T> {

    private boolean success = false;

    private int code = 0;

    private T data;

    private String msg;



    private ApiResult() {
    }

    public static ApiResult success(){
        ApiResult objectApiResult = new ApiResult<>();
        objectApiResult.code = 0;
        objectApiResult.success = true;
        return objectApiResult;
    }
    public static ApiResult success(String msg){
        ApiResult objectApiResult = new ApiResult<>();
        objectApiResult.code = 0;
        objectApiResult.success = true;
        objectApiResult.msg = msg;
        return objectApiResult;
    }

    public static <T> ApiResult<T> successWithData(T data){
        ApiResult objectApiResult = new ApiResult();
        objectApiResult.code = 0;
        objectApiResult.success = true;
        objectApiResult.data = data;
        return objectApiResult;
    }

    public static ApiResult fail(String msg){
        ApiResult objectApiResult = new ApiResult<>();
        objectApiResult.code = 1;
        objectApiResult.success = false;
        objectApiResult.msg = msg;
        return objectApiResult;
    }

    public static ApiResult fail(ApiResultEnum apiResultEnum){
        ApiResult objectApiResult = new ApiResult<>();
        objectApiResult.code = 1;
        objectApiResult.success = false;
        objectApiResult.msg = apiResultEnum.name();
        return objectApiResult;
    }





    public String getMsg() {
        return this.msg;
    }

    public ApiResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public ApiResult<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public int getCode() {
        return this.code;
    }

    public ApiResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public ApiResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "msg='" + msg + '\'' +
                ", success=" + success +
                ", code=" + code +
                ", data=" + data +
                '}';
    }
}
